<?
    /**
     * Smarty plugin
     * @package Smarty
     * @subpackage plugins
     * @author Kulikov Alexey <a.kulikov@gmail.com>
     */


    /**
     *
     */
    function smarty_function_load_mugs($params, &$smarty){
        global $db; // create a link to the database connection

        //pre-process parameters
        //variable name
        if(empty($params['var'])){
            $var = 'mugs';
        }else{
            $var = $params['var'];
        }

        $mugs = $db->getAll("SELECT 
                                    pm.created          AS intime,
                                    pm.title            AS title,
                                    pm.comment_count    AS comments,
                                    
                                    pm.id AS karmaid,
                                    page_karma.karma AS karma,
                                    pha.karma AS karmed,
                    
                                    ni.*,
                                    get_path(pm.nodeid)||pm.pageurl||'.html' AS url
                                FROM pagemap AS pm 
                                LEFT JOIN node_mug AS ni ON pm.pageid = ni.id                                
                                LEFT JOIN page_karma ON pm.id = page_karma.pageid
                                LEFT JOIN page_karma_history_anon pha ON pm.id = pha.pageid AND pha.ip = ".(int)$_SESSION['user']."                                
                                WHERE pm.pagetype = (SELECT id FROM datatype WHERE codeset = 'mug') AND ni.image = TRUE AND ni.id != ".(int)$params['exclude']."
                                ".($filter?" AND ".$filter:"")." ORDER BY RANDOM()
                                LIMIT ".($params['items']?$params['items']:3));

        //give all this shit to smarty
        $smarty->assign($var,$mugs);        
    }
?>